package info.hexin.json.serialize.render;

import info.hexin.json.serialize.JsonRender;
import info.hexin.json.serialize.JsonSerializeConfig;
import info.hexin.json.serialize.StringWrite;

import java.util.Map;

/**
 * 解析map
 * 
 * @author hexin
 * 
 */
public class MapRender implements JsonRender {
    public static MapRender instance = new MapRender();

    @Override
    public void render(Object object, StringWrite write) {
        write.append('{');
        Map<?, ?> map = (Map<?, ?>) object;
        int i = 0;
        int sum = map.size();
        for (Object key : map.keySet()) {
            String keyString = (String) key;
            write.appendFiledNameWithQuotation(keyString);
            write.append(':');
            JsonRender jsonRender = JsonSerializeConfig.getRender(map.get(key).getClass());
            jsonRender.render(map.get(key), write);
            if (++i < sum) {
                write.append(',');
            }
        }
        write.append('}');
    }
}
